package com.longma.server.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.longma.server.dao.AlarmProcessDAO;
import com.longma.server.entity.AlarmProcess;
import com.longma.server.pojo.dto.AlarmProcessAddDTO;
import com.longma.server.service.IAlarmProcessService;
import com.longma.server.service.IAlarmRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HAIBO
 * @since 2021-04-28
 */
@Transactional
@Service
public class AlarmProcessServiceImpl implements IAlarmProcessService {

    @Autowired
    private AlarmProcessDAO alarmProcessDAO;

    @Autowired
    private IAlarmRecordService alarmRecordService;

    /**
     * 新增告警处理单
     */
    @Override
    public AlarmProcess addAlarmProcess(AlarmProcessAddDTO addRequestDTO) {
        // 复制
        AlarmProcess alarmProcess = new AlarmProcess();
        BeanUtil.copyProperties(addRequestDTO,alarmProcess);
        alarmProcessDAO.save(alarmProcess);

        // 根据告警记录id处理告警记录  将告警记录标志为已处理，如果该设备没有告警记录 云仓标记为正常运行
        alarmRecordService.handleAlarmRecord(alarmProcess.getAlarmRecordId());

        return alarmProcess;
    }


    /**
     *  根据告警id获取告警告警处理单
     */
    @Override
    public AlarmProcess getAlarmProcessByAlarmRecordId(int id) {
        return alarmProcessDAO.getOne(Wrappers.<AlarmProcess>lambdaQuery().eq(AlarmProcess::getAlarmRecordId,id));
    }

    /**
     *  根据告警处理单id获取告警告警处理单
     */
    @Override
    public AlarmProcess getAlarmProcessById(int id) {
        return alarmProcessDAO.getById(id);
    }

}
